package com.zjh.webShell.controller;

import com.zjh.common.entity.Response;
import com.zjh.webShell.DAO.ConnectServerInfo;
import com.zjh.webShell.service.ConnectServerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/WebShell/connectServer")
@CrossOrigin(origins = "http://localhost:5173")
public class ConnectServerController {

    @Resource
    private ConnectServerService connectServerService;

    /**
     * 从库中获取所有连接
     * TODO: 根据用户确定返回结果
     * @return
     */
    @GetMapping("/getAllConnectList")
    public Response<List<ConnectServerInfo>> getAllConnectList(){
        return Response.success(connectServerService.list());
    }

    /**
     * 新增一条连接信息
     */
    @PostMapping("/addItem")
    public Response addItem(@RequestBody ConnectServerInfo connectServerInfo){

        boolean save = connectServerService.save(connectServerInfo);
        if(save){
            return Response.success("添加成功");
        }else {
            return Response.error("添加失败");
        }

    }

    /**
     * 根据Id删除一条连接信息
     */
    @PostMapping("/deleteItem")
    public Response deleteItem(@RequestParam("Id") String Id){
        boolean b = connectServerService.removeById(Id);
        if(b){
            return Response.success("删除成功");
        }else {
            return Response.error("删除失败");
        }
    }

    /**
     * 修改连接信息
     */
    @PostMapping("/updateItem")
    public Response updateItem(@RequestBody ConnectServerInfo connectServerInfo){

        boolean b = connectServerService.saveOrUpdate(connectServerInfo);

        if(b){
            return Response.success("更新信息成功");
        }else {
            return Response.error("更新信息失败");
        }

    }

}
